package com.nervenets.general.annotation;

import com.google.common.collect.Lists;
import com.nervenets.general.service.BaseService;

import java.lang.annotation.*;
import java.util.List;

/**
 * 表单定义描述
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface FormDefinition {
    /**
     * @return 字段说明
     */
    String value();

    /**
     * @return 描述
     */
    String desc() default "";

    /**
     * @return 是否必填
     */
    boolean required() default false;

    /**
     * @return 输入类型
     */
    FormDefinition.InputType inputType() default InputType.input;

    /**
     * @return 字段分组
     */
    String group() default "";

    /**
     * @return 是否隐藏
     */
    boolean hidden() default false;

    /**
     * @return 默认值
     */
    String def() default "";

    /**
     * @return 最大长度
     */
    int maxLength() default 0;

    /**
     * @return 选择组件的选项获取Service，返回 #com.nervenets.general.entity.Selection 的数组
     */
    Class<? extends BaseService> selectionService() default BaseService.class;

    /**
     * @return 选择组件的选项获取方法，返回 #com.nervenets.general.entity.Selection 的数组
     */
    String selectionMethod() default "";

    enum InputType {
        radio,
        checkbox,
        input,
        inputPassword,
        inputTextarea,
        inputNumber,
        select,
        cascader,
        switchButton,
        slider,
        timeSinglePicker,
        dateSinglePicker,
        dateTimeSinglePicker,
        timeRangePicker,
        dateRangePicker,
        dateTimeRangePicker,
        file,
        files,
        colorPicker,
        location,
        object;

        public static List<InputType> selectionTypes() {
            return Lists.newArrayList(radio, checkbox, select, cascader);
        }

        public static InputType customOf(String dataType) {
            try {
                return InputType.valueOf(dataType);
            } catch (Exception ignored) {
            }
            return input;
        }
    }
}
